﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using DC2010;
using Microsoft.Xna.Framework;
using DC2010.Objects;
using Microsoft.Xna.Framework.Input;
using Xna.BaseObjects;
using Xna.Base;
using DC2012.Screens;
using RolePlaying.GameScreens.Render;

namespace RolePlaying.GameScreens
{
    public class DungeonScreen
    {
        private ScreenManager screenManager;

        private Vector2 pos_position = new Vector2(10f,10f);
        private Vector2 pos_log = new Vector2(10f, 450f);

        public Vector2 pos_origin = new Vector2(5f, 100f);

        AnimatingSprite screenAnimation = new AnimatingSprite();

        //Creates a dungeon screen with related screen manager
        public DungeonScreen(ScreenManager screenManager)
        {
            // check the parameter
            if (screenManager == null)
            {
                throw new ArgumentNullException("screenManager");
            }
            this.screenManager = screenManager;
            pos_origin = RM.Get("Dungeon").GetVector();
        }

        public void LoadContent()
        {
            Root.I.LoadingStatus = "initializing content...Dungeon";

            ContentManager content = screenManager.Game.Content;

            //WallSet wsDefault = new WallSet("default", content);
            //WallSets.Add(wsDefault.Name, wsDefault);

        }

        /// <summary>
        /// FSP solution from here: http://www.david-amador.com/2009/11/how-to-do-a-xna-fps-counter/
        /// </summary>
        int _total_frames = 0;
        float _elapsed_time = 0.0f;
        int _fps = 0;
        public void Update(GameTime gameTime)
        {
            //screenAnimation.UpdateAnimation((float)gameTime.ElapsedGameTime.TotalSeconds);
            // Update
            _elapsed_time += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
 
            // 1 Second has passed
            if (_elapsed_time >= 1000.0f)
            {
                _fps = _total_frames;
                _total_frames = 0;
                _elapsed_time = 0;
            }

        }



        public void Draw(GameTime gameTime)
        {
            //draw only if we know, we are rdy.
            if (Root.I.GameSystem != null)
            {
                SpriteBatch spriteBatch = screenManager.SpriteBatch;
                DateTime dtStart = DateTime.Now;


                spriteBatch.Begin();
                DrawBackGround();
                spriteBatch.End();

                RasterizerState rs = new RasterizerState();
                rs.ScissorTestEnable = true;

                spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, rs, null);
                //spriteBatch.GraphicsDevice.RenderState.ScissorTestEnable = true;
                spriteBatch.GraphicsDevice.ScissorRectangle = RM.Get("Dungeon").rect;

                DrawDungeon(spriteBatch);
                spriteBatch.End();


                spriteBatch.Begin();
                //spriteBatch.GraphicsDevice.RenderState.ScissorTestEnable = false;
                DrawTexts();

                //DrawCoordinates();

                DateTime dtEnd = DateTime.Now;
                double d = (dtEnd - dtStart).TotalMilliseconds;
                DrawFPS(d);

                Vector2 idlePortraitPosition = new Vector2(300f, 380f);

                spriteBatch.End();
            }
        }

        private void DrawTexts()
        {
            SpriteBatch spriteBatch = screenManager.SpriteBatch;

            if (Root.I.GameSystem != null)
            {
                spriteBatch.DrawString(Fonts.DebugFont,
                "X: " + Root.I.GameSystem.Party.Location.X +
                "   Y:" + Root.I.GameSystem.Party.Location.Y +
                "   FACING:" + Root.I.GameSystem.Party.Location.Facing.ToString().ToUpper(),
                pos_log, Color.Blue);

                spriteBatch.DrawString(Fonts.DebugFont,
                Root.I.Log.ToUpper(),
                pos_log, Color.Blue);
            }

        }
        private double lastFPS = 0;
        private void DrawFPS(double timeToRender)
        {
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            _total_frames++;

            if (timeToRender > 0)
            {
                lastFPS = (int)(1000 / timeToRender);
            }
            spriteBatch.DrawString(Fonts.DebugFont,
            "FPS: " + _fps,
            new Vector2(0, 0), Color.White);
        }



        private void DrawBackGround()
        {
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            spriteBatch.Draw(TM.GetTex("Interface/Background"), RM.Get("Background").rect, Color.White);
        }

        private void DrawDungeon(SpriteBatch spriteBatch)
        {
            DrawPre(spriteBatch);
			Draw5(spriteBatch);
            Draw4(spriteBatch);
            Draw3(spriteBatch);
            Draw2(spriteBatch);
            Draw1(spriteBatch);
            Draw0(spriteBatch);
            DrawAfter(spriteBatch);
        }

        private void DrawAfter(SpriteBatch spriteBatch)
        {
            
        }

        private void DrawPre(SpriteBatch spriteBatch)
        {
            //SpriteBatch spriteBatch = screenManager.SpriteBatch;
            //
            DungeonScreenHelper.Draw(spriteBatch, TM.GetTex("WallSets/Grey_big/Ceiling"), new Vector2(0, 0), pos_origin, new Vector2(1, 1), Color.White);
            DungeonScreenHelper.Draw(spriteBatch, TM.GetTex("WallSets/Grey_big/Floor"), new Vector2(0, 544 - 338), pos_origin, new Vector2(1, 1), Color.White);
        }

        private void Draw0(SpriteBatch spriteBatch)
        {
            DungeonScreenHelper.DrawUniversal(-1, 0, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(1, 0, spriteBatch, pos_origin);
            //DungeonScreenHelper.DrawUniversal(0, 0, spriteBatch, pos_origin);
 
        }


        private void Draw1(SpriteBatch spriteBatch)
        {
            DungeonScreenHelper.DrawUniversal(-1, -1, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(1, -1, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(0, -1, spriteBatch, pos_origin);
        }

        private void Draw2(SpriteBatch spriteBatch)
        {
            DungeonScreenHelper.DrawUniversal(-2, -2, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(-1, -2, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(2, -2, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(1, -2, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(0, -2, spriteBatch, pos_origin);
        }

        private void Draw3(SpriteBatch spriteBatch)
        {
            DungeonScreenHelper.DrawUniversal(-2, -3, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(-1, -3, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(2, -3, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(1, -3, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(0, -3, spriteBatch, pos_origin);
        }

        private void Draw4(SpriteBatch spriteBatch)
        {
            DungeonScreenHelper.DrawUniversal(-3, -4, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(-2, -4, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(-1, -4, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(3, -4, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(2, -4, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(1, -4, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(0, -4, spriteBatch, pos_origin);
        }
		
        private void Draw5(SpriteBatch spriteBatch)
        {
            DungeonScreenHelper.DrawUniversal(-2, -5, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(-1, -5, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(2, -5, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(1, -5, spriteBatch, pos_origin);
            DungeonScreenHelper.DrawUniversal(0, -5, spriteBatch, pos_origin);
        }		
    }
}
